<template>
	<view class="QS-CountDown">
		<block v-if="['day'].includes(fields) && Number(getCountDownObj.day)">
			<view class="count-down-item" :style="{
				padding: padding,
				backgroundColor: backgroundColor
			}">
				<text class="count-down-item-text" style="color: #FFFFFF;">{{getCountDownObj.day || '00'}}</text>
			</view>
			<text class="count-down-item-placeholder" :style="{ color: backgroundColor }">天</text>
		</block>
		<block v-if="['day', 'hour'].includes(fields)">
			<view class="count-down-item" :style="{
				padding: padding,
				backgroundColor: backgroundColor
			}">
				<text class="count-down-item-text" style="color: #FFFFFF;">{{getCountDownObj.hour || '00'}}</text>
			</view>
			<text class="count-down-item-placeholder" :style="{ color: backgroundColor }">:</text>
		</block>
		<block v-if="['day', 'hour', 'minute'].includes(fields)">
			<view class="count-down-item" :style="{
				padding: padding,
				backgroundColor: backgroundColor
			}">
				<text class="count-down-item-text" style="color: #FFFFFF;">{{getCountDownObj.minute || '00'}}</text>
			</view>
			<text class="count-down-item-placeholder" :style="{ color: backgroundColor }">:</text>
		</block>
		<block v-if="['day', 'hour', 'minute', 'second'].includes(fields)">
			<view class="count-down-item" :style="{
				padding: padding,
				backgroundColor: backgroundColor
			}">
				<text class="count-down-item-text" style="color: #FFFFFF;">{{getCountDownObj.second || '00'}}</text>
			</view>
		</block>
	</view>
</template>

<script>
	export default {
		props: {
			padding: {
				type: String,
				default: '8rpx'
			},
			time: {
				type: [Number, String],
				default: +new Date()
			},
			backgroundColor: {
				type: String,
				default: '#F52F50'
			},
			id: {
				type: String,
				default: ''
			},
			fields: {
				type: String,
				default: 'day'
			},
			initTime: {
				type: Boolean,
				default: true
			},
			customCountDownObj: {
				type: Object,
				default: ()=>({})
			}
		},
		watch: {
			time() {
				this.init();
			}
		},
		data() {
			return {
				countDownObj: {}
			}
		},
		computed: {
			getCountDownObj() {
				if(this.initTime) return this.countDownObj;
				
				return this.customCountDownObj;
			}
		},
		mounted() {
			this.init();
		},
		methods: {
			init() {
				if(this.initTime) 
					this.countDownObj = uni.$qs.countDown({
						id: this.id,
						beginDate: this.time,
						fields: this.fields
					});
			},
			clear() {
				if(this.countDownObj) {
					if(this.countDownObj?.stop && typeof this.countDownObj.stop == 'function') this.countDownObj.stop();
					this.countDownObj = null
				}
			}
		},
		beforeDestroy() {
			this.clear();
		}
	}
</script>

<style scoped>
	.QS-CountDown{
		display: flex;
		flex-direction: row;
		white-space: nowrap;
	}
	.count-down-item{
		border-radius: 4rpx;
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		height: 36rpx;
		min-width: 36rpx;
	}
	.count-down-item-text{
		font-size: 22rpx;
		font-weight: bold;
	}
	.count-down-item-placeholder{
		font-size: 22rpx;
		margin-left: 8rpx;
		margin-right: 8rpx;
	}
</style>
